
const express = require('express');
const bodyParser = require("body-parser");

const { getlist, add, del, modify } = require('./js/mothods')

//创建application/x-www-form-urlencoded
const urlencodedParser = bodyParser.urlencoded({extended: false});
const app = express();
app.use(express.static('public')) 

app.listen(8081,function(){
	console.log("服务已启动。。。");
});



// 获取列表
app.get('/api/findUsers', function(req, res){
    let responseData = getlist();
    console.log('返回数据', responseData)
	res.send(responseData)
})

// 添加
app.post('/api/addUser', urlencodedParser, function(req, res){
    let responseData = add(req.body);
    res.send({status: 'ok'});
})

// 删除
app.post('/api/removeUser', urlencodedParser, function(req, res){
    let responseData = del(req.body.id);
    res.send({status: 'ok'});
})

// 修改
app.post('/api/saveUser', urlencodedParser, function(req, res){
    let responseData = modify(req.body);
    res.send({status: 'ok'});
})

